package generator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.SqlMapGeneratorConfiguration;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class RunMBG {

	@Before
	public void setUp() throws Exception {
	}
	@Test
	public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException,
	        InterruptedException {
		String file = "generator.xml";
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		Resource re = new ClassPathResource(file);
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration configuration = cp.parseConfiguration(re.getInputStream());
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
		myBatisGenerator.generate(null);
	}
	@Test
	public void testJDbc() throws IOException, XMLParserException, InvalidConfigurationException, SQLException,
	        InterruptedException {
		String file = "generatorJdbc.xml";
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		Resource re = new ClassPathResource(file);
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration configuration = cp.parseConfiguration(re.getInputStream());
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
		myBatisGenerator.generate(null);
	}

	@Test
	public void testJDbcJava() throws IOException, InvalidConfigurationException, SQLException, InterruptedException {
		List<String> warnings = new ArrayList<String>();
		//参照  org.mybatis.generator.config.xml.MyBatisGeneratorConfigurationParser 进行配置生成
		Configuration configuration = new Configuration();
		// classPathEntry
		configuration
		        .addClasspathEntry("D:/m2/repository/mysql/mysql-connector-java/5.1.19/mysql-connector-java-5.1.19.jar");
		// context
		Context context = new Context(null);
		context.setId("context1");
		configuration.addContext(context);
		// commentGenerator
		CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
		commentGeneratorConfiguration.addProperty("suppressDate", "false");
		commentGeneratorConfiguration.addProperty("suppressAllComments", "true");
		context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
		// jdbcConnection
		JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
		jdbcConnectionConfiguration.setDriverClass("com.mysql.jdbc.Driver");
		jdbcConnectionConfiguration.setConnectionURL("jdbc:mysql://127.0.0.1:3306/test");
		jdbcConnectionConfiguration.setUserId("root");
		jdbcConnectionConfiguration.setPassword("root");
		context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
		// javaModelGenerator
		JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
		javaModelGeneratorConfiguration.setTargetPackage("toplevelpackage.app2.gen.model");
		javaModelGeneratorConfiguration.setTargetProject("src/main/java");
		javaModelGeneratorConfiguration.addProperty("enableSubPackages", "true");
		javaModelGeneratorConfiguration.addProperty("trimStrings", "true");
		context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
		// sqlMapGenerator
		SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
		context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
		// javaClientGenerator
		JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
		javaClientGeneratorConfiguration
		        .setConfigurationType("org.mybatis.generator.codegen.spring.jdbc.SpringJdbcClientGenerator");
		javaClientGeneratorConfiguration.setTargetPackage("toplevelpackage.app2.gen.dao");
		javaClientGeneratorConfiguration.setTargetProject("src/main/java");
		// javaClientGeneratorConfiguration.setImplementationPackage(implementationPackage);
		javaClientGeneratorConfiguration.addProperty("enableSubPackages", "true");
		context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);

		// table 每表一个 TableConfiguration
		TableConfiguration tc = new TableConfiguration(context);
		 tc.setTableName("qrtz_job_details_log");
		 tc.setDomainObjectName("QrtzJobDetailsLog");
		 tc.setCountByExampleStatementEnabled(false);
		 tc.setUpdateByExampleStatementEnabled(false);
		 tc.setDeleteByExampleStatementEnabled(false);
		 tc.setSelectByExampleStatementEnabled(false);
//		 tc.setSelectByExampleQueryId("id");
		 context.addTableConfiguration(tc);

		DefaultShellCallback callback = new DefaultShellCallback(true);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
		myBatisGenerator.generate(null);
	}
}
